
#加密工具
from itsdangerous.jws import TimedJSONWebSignatureSerializer as TJWSSerializer
from django.conf import settings

#1.加密openid
def encode_openid(openid):
    #1.1创建加密对象
    serializer=TJWSSerializer(secret_key=settings.SECRET_KEY,expires_in=300)
    #1.2加密数据
    token=serializer.dumps({'openid':openid})

    #返回解码后的响应
    return token.decode()

#2.解密视频
def decode_openid(data):
    #1.1创建解密对象,解密对象密钥要跟加密的密钥一致
    serializer=TJWSSerializer(secret_key=settings.SECRET_KEY,expires_in=300)

    #1.2解密数据,因为可能会过了有效期,所以要用try
    try:
        dict_data=serializer.loads(data)
    except Exception as e:
        return None

    #1.3返回响应
    return dict_data.get('openid')

